package org.jeecg.modules.workorder.service.impl;

import java.util.List;

import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.workorder.entity.WorkingOrderRecord;
import org.jeecg.modules.workorder.mapper.WorkingOrderRecordMapper;
import org.jeecg.modules.workorder.service.IWorkingOrderRecordService;
import org.jeecg.modules.workorder.service.IWorkingOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 生产工单记录
 * @Date:   2021-09-09
 * @Version: V1.0
 */
@Service
public class WorkingOrderServiceRecordImpl extends ServiceImpl<WorkingOrderRecordMapper, WorkingOrderRecord> implements IWorkingOrderRecordService {

	@Autowired
	WorkingOrderRecordMapper workingOrderRecordMapper;
	
	@Override
	public boolean addWorkingOrderRecord(WorkingOrderRecord workingOrderRecord) {
	   //新增时设置hasChild为0
		workingOrderRecord.setHasChild(IWorkingOrderService.NOCHILD);
		if(oConvertUtils.isEmpty(workingOrderRecord.getParentId())){
			workingOrderRecord.setParentId(IWorkingOrderService.ROOT_PID_VALUE);
		}else{
			//如果当前节点父ID不为空 则设置父节点的hasChildren 为1
			WorkingOrderRecord parent = baseMapper.selectById(workingOrderRecord.getParentId());
			if(parent!=null && !"1".equals(parent.getHasChild())){
				parent.setHasChild("1");
				baseMapper.updateById(parent);
			}
		}
		int count = baseMapper.insert(workingOrderRecord);
		return count > 0 ? true : false;
	}
	
	

	@Override
	public IPage<WorkingOrderRecord> page(Page<WorkingOrderRecord> page,
			QueryWrapper<WorkingOrderRecord> queryWrapper) {
		return workingOrderRecordMapper.selectWorkingOrderRecordPage(page, queryWrapper);// 分页对象
	}

	@Override
	public List<WorkingOrderRecord> queryTreeListNoPage(QueryWrapper<WorkingOrderRecord> queryWrapper) {
		List<WorkingOrderRecord> dataList = workingOrderRecordMapper.selectWorkingOrderRecordList(queryWrapper);
        return dataList;
	}
	
}
